//
//  UIView+TYRoundRect.m
//  TeacherApp
//
//  Created by 谢航宇 on 2019/5/9.
//  Copyright © 2019 chuanfeng. All rights reserved.
//

#import "UIView+TYRoundRect.h"

@implementation UIView (TYRoundRect)
- (void)setRoundRect:(UIRectCorner)corners cornerRadius:(CGFloat)cornerRadius {  
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = cornerRadius;
        self.layer.maskedCorners = (CACornerMask)corners;
    } else {
        dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.15f * NSEC_PER_SEC));
        dispatch_after(delayTime, dispatch_get_main_queue(), ^{
            UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(cornerRadius, cornerRadius)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = self.bounds;
            maskLayer.path = path.CGPath;
            self.layer.mask = maskLayer;
        });
        
    }
}

- (void)layoutSubviews {
    for (UIView * view in self.subviews) {
        if (view.layer.mask && [view.layer.mask isKindOfClass:[CAShapeLayer class]]) {
            CAShapeLayer *maskLayer = view.layer.mask;
            maskLayer.frame = view.bounds;
        }
    } 
}
@end
